package co.com.desarrollo.pedidos.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import co.com.desarrollo.pedidos.dao.RolDAO;
import co.com.desarrollo.pedidos.entities.Rol;

@Repository("rolDAO")
public class JpaRolDAOImpl implements RolDAO {

	@PersistenceContext
	private EntityManager entityManager;
	
	@Transactional
	public void delete(long rolId) {
		entityManager.remove(entityManager.find(Rol.class, rolId));
	}

	@Transactional(readOnly = true)
	public List<Rol> findAll() {
		return entityManager.createQuery("from Rol",Rol.class).getResultList();
	}

	@Transactional(readOnly = true)
	public Rol findById(long rolId) {		
		return entityManager.find(Rol.class, rolId);
	}

	@Transactional
	public Rol store(Rol rol) {
		return entityManager.merge(rol);
	}

	@Transactional(readOnly = true)
	public Rol findByName(String nombre) {
		return entityManager.createQuery("from Rol r where r.nombre = '"+nombre+"'", Rol.class).getSingleResult();
	}

}
